//
//  YFSeletePhotoManager.m
//  CoollangTennisBall
//
//  Created by Coollang on 2017/9/12.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFSeletePhotoManager.h"
#import <TZImagePickerController/TZImagePickerController.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
#import "YFFullScreenCameraViewController.h"
#import <TZImagePickerController/TZGifPhotoPreviewController.h>
#import <TZImagePickerController/TZVideoPlayerController.h>
#import <TZImagePickerController/TZImageManager.h>

@interface YFSeletePhotoManager ()<TZImagePickerControllerDelegate,YFFullScreenCameraViewControllerDelegate>
@property (nonatomic, assign)BOOL isSelectOriginalPhoto; // 是否是原图
@property (nonatomic, strong) NSMutableArray *selectedAssets;
@property (nonatomic, strong) NSMutableArray <UIImage *>*selectedPhotos;
@end

@implementation YFSeletePhotoManager

#pragma mark - 获取照片
- (void)pushImagePickerController:(UIViewController *)targetVc {
    
    TZImagePickerController *imagePickerVc = [self tZImagePickerController];
    
#pragma mark - 到这里为止
    // 你可以通过block或者代理，来得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        _selectedAssets = [NSMutableArray arrayWithArray:assets];
        _selectedPhotos = [NSMutableArray arrayWithArray:photos];
        
        _isSelectOriginalPhoto = isSelectOriginalPhoto;
        if ([self.delegate respondsToSelector:@selector(seletePhotoDidFinishPickingPhotos:assets:isOriginalPhoto:)]) {
            [self.delegate seletePhotoDidFinishPickingPhotos:self.selectedPhotos assets:self.selectedAssets isOriginalPhoto:self.isSelectOriginalPhoto];
        }
    }];
    
    imagePickerVc.navigationBar.tintColor = targetVc.navigationController.navigationBar.tintColor;
    
    imagePickerVc.navigationBar.barTintColor = targetVc.navigationController.navigationBar.barTintColor;    [imagePickerVc.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],
                                                                                                                                                              NSFontAttributeName: [UIFont systemFontOfSize:20.0f]
                                                                                                                                                              }];
    imagePickerVc.navigationBar.alpha = 1.0;
    [targetVc presentViewController:imagePickerVc animated:YES completion:nil];
}


- (void)showPickerControllerWithType:(kTapActionButtonType)tapType targetVc:(UIViewController *)targetVc {
    
    if(tapType == kTapAlbumButton){
        [self pushImagePickerController:targetVc];
    }else if (tapType == kTapCameraButton){
        YFFullScreenCameraViewController *fullVc =  [[YFFullScreenCameraViewController alloc] init];
        fullVc.type = YFCameraTypePhoto;
        fullVc.delegate = self;
        fullVc.maxRecordTime = 10;
        [targetVc presentViewController:fullVc animated:YES completion:nil];
    }
}

- (void)fullScreenCameraDidFinishClick:(YFRecordPhotoModel *)model {
    NSLog(@"%@",model);
    [self.selectedAssets addObject:model.tzAssetModel.asset];
    [self.selectedPhotos addObject:model.previewPhoto];
    
    if ([self.delegate respondsToSelector:@selector(seletePhotoDidFinishPickingPhotos:assets:isOriginalPhoto:)]) {
        [self.delegate seletePhotoDidFinishPickingPhotos:self.selectedPhotos assets:self.selectedAssets isOriginalPhoto:self.isSelectOriginalPhoto];
    }
}

+ (UIImage *)fullResolutionImageFromALAsset:(ALAsset *)asset
{
    ALAssetRepresentation *assetRep = [asset defaultRepresentation];
    CGImageRef imgRef = [assetRep fullResolutionImage];
    UIImage *img = [UIImage imageWithCGImage:imgRef
                                       scale:assetRep.scale
                                 orientation:(UIImageOrientation)assetRep.orientation];
    return img;
}

// 照片预览
- (void)priviewSeletePhoto:(NSInteger)index showParentVc:(UIViewController *)patentVc {
    id asset = _selectedAssets[index];
    BOOL isVideo = NO;
    if ([asset isKindOfClass:[PHAsset class]]) {
        PHAsset *phAsset = asset;
        isVideo = phAsset.mediaType == PHAssetMediaTypeVideo;
    } else if ([asset isKindOfClass:[ALAsset class]]) {
        ALAsset *alAsset = asset;
        isVideo = [[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo];
    }
    if ([[asset valueForKey:@"filename"] tz_containsString:@"GIF"]) {
        TZGifPhotoPreviewController *vc = [[TZGifPhotoPreviewController alloc] init];
        TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypePhotoGif timeLength:@""];
        vc.model = model;
        [patentVc presentViewController:vc animated:YES completion:nil];
    } else if (isVideo) { // perview video / 预览视频
        TZVideoPlayerController *vc = [[TZVideoPlayerController alloc] init];
        TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypeVideo timeLength:@""];
        vc.model = model;
        [patentVc presentViewController:vc animated:YES completion:nil];
    } else {
        // preview photos / 预览照片
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:index];
        imagePickerVc.maxImagesCount = 4;
        imagePickerVc.allowPickingOriginalPhoto = NO;
        imagePickerVc.isSelectOriginalPhoto = NO;
        
        [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
            _selectedPhotos = [NSMutableArray arrayWithArray:photos];
            _selectedAssets = [NSMutableArray arrayWithArray:assets];
            _isSelectOriginalPhoto = isSelectOriginalPhoto;
            if ([self.delegate respondsToSelector:@selector(seletePhotoDidFinishPickingPhotos:assets:isOriginalPhoto:)]) {
                [self.delegate seletePhotoDidFinishPickingPhotos:self.selectedPhotos assets:self.selectedAssets isOriginalPhoto:self.isSelectOriginalPhoto];
            }
        }];
        [patentVc presentViewController:imagePickerVc animated:YES completion:nil];
    }
}

- (TZImagePickerController *)tZImagePickerController {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:6 columnNumber:4 delegate:self pushPhotoPickerVc:YES];
#pragma mark - 四类个性化设置，这些参数都可以不传，此时会走默认设置
    imagePickerVc.isSelectOriginalPhoto = NO;
    
    imagePickerVc.selectedAssets = _selectedAssets; // 目前已经选中的图片数组
    imagePickerVc.allowTakePicture = NO; // 在内部显示拍照按钮
    
    // 3. 设置是否可以选择视频/图片/原图
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowPickingImage = YES;
    imagePickerVc.allowPickingGif = YES;
    imagePickerVc.allowPickingMultipleVideo = YES;
    
    // 4. 照片排列按修改时间升序
    imagePickerVc.sortAscendingByModificationDate = YES;
    return imagePickerVc;
}

- (NSMutableArray *)selectedAssets {
    if (_selectedAssets == nil) {
        _selectedAssets = [NSMutableArray array];
    }
    return _selectedAssets;
}
- (NSMutableArray *)selectedPhotos {
    if (_selectedPhotos == nil) {
        _selectedPhotos = [NSMutableArray array];
    }
    return _selectedPhotos;
}

// 是否存在视频
- (BOOL)isExistVideo {
    for (id asset in self.selectedAssets) {
        if([YFSeletePhotoManager isVideoWithAsset:asset]){
            return YES;
        }
    }
    return NO;
}

+ (BOOL)isVideoWithAsset:(id)asset {
    BOOL isVideo = NO;
    if ([asset isKindOfClass:[PHAsset class]]) {
        PHAsset *phAsset = asset;
        isVideo = phAsset.mediaType == PHAssetMediaTypeVideo;
    } else if ([asset isKindOfClass:[ALAsset class]]) {
        ALAsset *alAsset = asset;
        isVideo = [[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo];
    }
    return isVideo;
}

@end
